package com.record.controller;

import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.record.entity.Agrees;
import com.record.entity.AgreesRelevance;
import com.record.entity.FarmEnvironmental;
import com.record.entity.FarmInfo;
import com.record.entity.FarmerInfo;
import com.record.entity.FutureProductDetail;
import com.record.entity.FutureProductInfo;
import com.record.service.AgreesRelevanceService;
import com.record.service.AgreesService;
import com.record.service.FarmEnvironmentalService;
import com.record.service.FarmInfoService;
import com.record.service.FarmerInfoService;
import com.record.service.FutureProductDetailService;
import com.record.service.FutureProductInfoService;
import com.record.service.impl.FarmInfoServiceImpl;
import com.record.service.impl.FarmerInfoServiceImpl;

@Controller
@RequestMapping("/pc/record")
public class RecordController {

	@Resource 
	private FarmInfoService farmInfoServiceImpl;
	@Resource 
	private FarmerInfoService farmerInfoServiceImpl;
	@Resource 
	private FutureProductDetailService futureProductDetailServiceImpl;
	@Resource 
	private FutureProductInfoService futureProductInfoServiceImpl;
	@Resource 
	private FarmEnvironmentalService farmEnvironmentalServiceImpl;
	@Resource 
	private AgreesService agreesServiceImpl;
	@Resource
	private  AgreesRelevanceService agreesRelevanceServiceImpl;
	

	/**
	 * 查看植物履历
	 * @param furId 产品id
	 * @return
	 */
	@RequestMapping(value="/toLook", method={RequestMethod.GET,RequestMethod.POST})
	public String toRecord(int furId,ModelMap model,HttpServletRequest request){
		
		FutureProductInfo futInfo = futureProductInfoServiceImpl.selectByPrimaryKey(furId);//商品信息
		FarmerInfo farmerInfo = farmerInfoServiceImpl.selectByPrimaryKey(futInfo.getFurInfoId());//获取农户经纬度
		FarmInfo farm = farmInfoServiceImpl.selectByPrimaryKey(farmerInfo.getFarmId());//获取农场名称
		List<FutureProductDetail> furDyList = futureProductDetailServiceImpl.selectListByfurId(furId);//商品履历
		//是否点赞
		int isLike;
		//点赞记录
		AgreesRelevance AgreesNote = new AgreesRelevance();
		String userip = getIpAddr(request);//获取ip 
		AgreesNote.setUserip(userip);
		AgreesNote.setFurId(furId);
		AgreesRelevance note = agreesRelevanceServiceImpl.selectAgreeByBase(AgreesNote);
		if(note==null){
			isLike=0;//没有点赞记录
		}else{
			isLike=1;//已有点赞记录
		}
		//获取点赞次数
		Agrees agree = agreesServiceImpl.selectAgreeByFurId(furId);//获取点赞次数
		Collections.reverse(furDyList);
		model.addAttribute("farmerInfo", farmerInfo);//农户经纬度
		model.addAttribute("futInfo", futInfo);//植物信息
		model.addAttribute("furId", furId);//植物ID
		model.addAttribute("farm", farm);//农场
		model.addAttribute("furDyList", furDyList);//履历列表
		model.addAttribute("agree", agree);//点赞次数
		model.addAttribute("isLike", isLike);//是否已经点赞
		return "/pc/record";
	}
	/**
	 * 农场主页
	 * @param farmId 农场id
	 * @param farmerId 农户id 
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/toMain", method={RequestMethod.GET,RequestMethod.POST})
	public String toMain(int farmId,int farmerId,ModelMap model){
		FarmInfo farm = farmInfoServiceImpl.selectByPrimaryKey(farmId);//农场信息
		FarmerInfo farmer = farmerInfoServiceImpl.selectByPrimaryKey(farmerId);//农场电话坐标
		List<FutureProductInfo> productList = futureProductInfoServiceImpl.selectListByfarmerId(farmerId);//农场所有产品
		Collections.reverse(productList);
		model.addAttribute("farm", farm);
		model.addAttribute("farmer", farmer);
		model.addAttribute("productList", productList);
		return "/pc/main";
	}
	/**
	 * 农产环境
	 * @param farmId 农场id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/farmEnvironment", method={RequestMethod.GET,RequestMethod.POST})
	public String toEnvironment(int farmId,ModelMap model){
		FarmEnvironmental farmEnvir = farmEnvironmentalServiceImpl.selectByfarmInfoId(farmId);
		FarmInfo farm = farmInfoServiceImpl.selectByPrimaryKey(farmId);
		model.addAttribute("farmEnvir", farmEnvir);
		model.addAttribute("farm", farm);
 		return "/pc/farmEnvironment";
	}
	/**
	 * 百度地图农场位置
	 * @param farmerId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/toLocation", method={RequestMethod.GET,RequestMethod.POST})
	public String toLocation(int farmerId,ModelMap model){
		FarmerInfo farmer = farmerInfoServiceImpl.selectByPrimaryKey(farmerId);
		model.addAttribute("farmer", farmer);
		return "/pc/location";
	}
	/**
	 * 点赞
	 * @param request
	 * @param model
	 * @param type
	 * @param fileName
	 * @param src
	 * @param memberid
	 * @param childInfoId
	 * @return
	 */
	@RequestMapping(value="/isGood",method= RequestMethod.POST)
	public @ResponseBody Map<String, Object> isGood(HttpServletRequest request,ModelMap model,int agree,int furId,int isLike){
		Map<String, Object> data = new HashMap<String, Object>();
		AgreesRelevance agreesRelevance = new AgreesRelevance();
		agreesRelevance.setFurId(furId);//商品id
		agreesRelevance.setUserip(getIpAddr(request));//设置用户ip
		if(isLike==0){//没有点赞记录
			agreesRelevance.setDate(new Date());
			agreesRelevanceServiceImpl.insert(agreesRelevance);
			//插入agree_relevance（furId）
			data.put("message", "已点赞");
			data.put("isLike", "1");
		}else{//已经有记录
			AgreesRelevance agreeNote = agreesRelevanceServiceImpl.selectAgreeByBase(agreesRelevance);
			//删除agree_relevance记录（furId）
			agreesRelevanceServiceImpl.deleteByfurId(agreeNote);
			data.put("message", "取消点赞");
			data.put("isLike", "0");
		}
		//更新（furId）点赞数量
		Agrees agree1 = agreesServiceImpl.selectAgreeByFurId(furId);
		agree1.setAgree(agree);
		agreesServiceImpl.updateByfurId(agree1);
		return data;
	}
	/**
	 * 获取用户ip
	 * @param request
	 * @return
	 */

	public String getIpAddr(HttpServletRequest request){
	  String ip = request.getHeader("x-forwarded-for");
	  if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
	  {
	    ip = request.getHeader("Proxy-Client-IP");
	  }
	  if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
	  {
	    ip = request.getHeader("WL-Proxy-Client-IP");
	  }
	  if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
	  {
	    ip = request.getRemoteAddr();
	  }
	  System.out.println(ip);
	  return ip;
	}
	
}
